import random
import string
import re

class PasswordGenerator:
    """密码生成器类，用于生成安全的随机密码"""
    
    @staticmethod
    def generate_password(length=16, include_uppercase=True, include_lowercase=True, 
                         include_numbers=True, include_symbols=True, exclude_similar=False):
        """
        生成随机密码
        
        参数:
            length (int): 密码长度
            include_uppercase (bool): 是否包含大写字母
            include_lowercase (bool): 是否包含小写字母
            include_numbers (bool): 是否包含数字
            include_symbols (bool): 是否包含特殊符号
            exclude_similar (bool): 是否排除相似字符 (如 1, l, I, 0, O)
            
        返回:
            str: 生成的密码
        """
        # 确保至少选择了一种字符类型
        if not any([include_uppercase, include_lowercase, include_numbers, include_symbols]):
            include_lowercase = True
        
        # 定义字符集
        chars = ""
        
        if include_uppercase:
            chars += string.ascii_uppercase
        if include_lowercase:
            chars += string.ascii_lowercase
        if include_numbers:
            chars += string.digits
        if include_symbols:
            chars += string.punctuation
        
        # 排除相似字符
        if exclude_similar:
            for char in "Il1O0":
                chars = chars.replace(char, "")
        
        # 确保密码长度至少为4
        length = max(4, length)
        
        # 生成密码
        password = "".join(random.choice(chars) for _ in range(length))
        
        return password
    
    @staticmethod
    def check_password_strength(password):
        """
        检查密码强度
        
        参数:
            password (str): 要检查的密码
            
        返回:
            tuple: (强度评分(0-100), 强度级别(弱/中/强/非常强), 改进建议)
        """
        if not password:
            return 0, "无", ["密码不能为空"]
        
        score = 0
        suggestions = []
        
        # 长度评分 (最多30分)
        length = len(password)
        if length < 8:
            score += length * 2
            suggestions.append("密码长度应至少为8个字符")
        elif length < 12:
            score += 16 + (length - 8) * 2
            suggestions.append("考虑使用12个或更多字符的密码")
        else:
            score += 24 + min(6, (length - 12))
        
        # 复杂度评分 (最多40分)
        if re.search(r'[A-Z]', password):
            score += 10
        else:
            suggestions.append("添加大写字母")
            
        if re.search(r'[a-z]', password):
            score += 10
        else:
            suggestions.append("添加小写字母")
            
        if re.search(r'[0-9]', password):
            score += 10
        else:
            suggestions.append("添加数字")
            
        if re.search(r'[^A-Za-z0-9]', password):
            score += 10
        else:
            suggestions.append("添加特殊字符")
        
        # 多样性评分 (最多30分)
        unique_chars = len(set(password))
        score += min(20, unique_chars * 2)
        
        if unique_chars < length / 2:
            suggestions.append("增加字符多样性，避免重复字符")
        
        # 常见模式检查
        common_patterns = [
            r'12345', r'qwerty', r'password', r'admin', r'welcome',
            r'abc123', r'123abc', r'111111', r'000000'
        ]
        
        for pattern in common_patterns:
            if re.search(pattern, password.lower()):
                score = max(0, score - 20)
                suggestions.append("避免使用常见密码模式")
                break
        
        # 确定强度级别
        if score < 40:
            strength = "弱"
        elif score < 60:
            strength = "中"
        elif score < 80:
            strength = "强"
        else:
            strength = "非常强"
        
        # 如果没有改进建议但分数不是满分，添加一个通用建议
        if not suggestions and score < 100:
            suggestions.append("尝试增加密码长度和复杂性")
        
        # 如果分数很高，清空建议
        if score >= 80:
            suggestions = []
        
        return score, strength, suggestions